package com.elinkway.infinitemovies.http.requesttask;

import android.content.Context;
import android.os.Bundle;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.async.RequestResultListener;
import com.elinkway.infinitemovies.bean.AdNetWorkTypeInterface;
import com.elinkway.infinitemovies.bean.NewAdDataBean;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.bigdata.BigDataUtils;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.NewAdDataParser;
import com.elinkway.infinitemovies.utils.MoviesUtils;
import com.elinkway.infinitemovies.utils.MyLocationManager;
import com.elinkway.infinitemovies.utils.NetWorkTypeUtils;
import com.elinkway.infinitemovies.utils.PhoneUtils;
import com.elinkway.infinitemovies.utils.SharePreferenceManager;
import com.elinkway.infinitemovies.utils.StringUtil;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.Utils;
import com.lvideo.http.bean.LVideoDataHull;

import java.util.Locale;

/**
 * Created by huangxiaojian on 2017/5/31.
 *
 * 广告新接口请求任务
 */

public class RequestAdTask extends
        MoviesHttpAsyncTask<NewAdDataBean> {

    private Context mContext;
    /**
     * 广告位标识
     */
    private String mAdplace;
    /**
     * 站点源
     */
    private String mSite;
    private RequestResultListener<NewAdDataBean> mListener;
    private String browserUa = "";

    /**
     * 非贴片广告的请求
     * @param context 上下文
     * @param adplace 广告位标识
     * @param listener 外层监听
     */
    public RequestAdTask(Context context, String adplace, RequestResultListener<NewAdDataBean> listener) {
        super(context);
        this.mContext = context;
        this.mAdplace = adplace;
        this.mListener = listener;
        this.browserUa = Utils.getBrowserUa(mContext);
    }

    /**
     * 贴片广告的请求
     * @param context 上下文
     * @param adplace 广告位标识
     * @param site 站点源，目前主要用于贴片广告
     * @param listener 外层监听
     */
    public RequestAdTask(Context context, String adplace, String site, RequestResultListener<NewAdDataBean> listener) {
        super(context);
        this.mContext = context;
        this.mAdplace = adplace;
        this.mSite = site;
        this.mListener = listener;
        this.browserUa = Utils.getBrowserUa(mContext);
    }

    public interface AD_PARAMS {
        String AD_PLACE = "adplace";
        String MODEL = "model";
        String VENDOR = "vendor";
        String IMEI = "imei";
        String ANDROIDID = "androidId";
        String OPERATOR = "operator";
        String SCREEN_WIDTH = "screenWidth";
        String SCREEN_HEIGHT = "screenHeight";
        String OS_VERSION = "osVersion";
        String GPS_ENABLE = "gpsEnable";
        String GPS_TIME_STAMP = "gpsTimestamp";
        String SITE = "site";
        String MAC = "mac";
        String DENSITY = "density";
        String CITY = "city";
        String NETWORK = "network";
        String LON = "lon";
        String LAT = "lat";
        String AUID = "auid";
        String ROOT = "root";
        String ORIENTATION = "orientation";
        String LANG = "lang";
        String BROWSER_UA = "browserUa";
    }

    private String getNetWorkType() {
        int networkType = NetWorkTypeUtils.getNetType();
        switch (networkType) {
            case NetWorkTypeUtils.NETTYPE_NO:
                return AdNetWorkTypeInterface.NO_NETWORK;
            case NetWorkTypeUtils.NETTYPE_2G:
                return AdNetWorkTypeInterface.DATA_2_G;
            case NetWorkTypeUtils.NETTYPE_3G:
                return AdNetWorkTypeInterface.DATA_3_G;
            case NetWorkTypeUtils.NETTYPE_4G:
                return AdNetWorkTypeInterface.DATA_4_G;
            case NetWorkTypeUtils.NETTYPE_WIFI:
                return AdNetWorkTypeInterface.WIFI;
            default:
                return AdNetWorkTypeInterface.NO_NETWORK;
        }
    }

    private Bundle buildCommonParams() {
        Bundle params = new Bundle();
        params.putString(AD_PARAMS.ANDROIDID, BigDataUtils.getAndroidId(mContext));
        params.putString(AD_PARAMS.AD_PLACE, mAdplace);
        params.putString(AD_PARAMS.MODEL, PhoneUtils.getModel());
        params.putString(AD_PARAMS.VENDOR, PhoneUtils.getManufacturer());
        params.putString(AD_PARAMS.IMEI, PhoneUtils.getIMEI(mContext));
        params.putString(AD_PARAMS.OPERATOR, PhoneUtils.getOperator(mContext));
        params.putString(AD_PARAMS.SCREEN_WIDTH, String.valueOf(UIs.getScreenWidth()));
        params.putString(AD_PARAMS.SCREEN_HEIGHT, String.valueOf(UIs.getScreenHeight()));
        params.putString(AD_PARAMS.OS_VERSION, String.valueOf(MoviesUtils.getOSVersion().replaceAll("[^\\d^\\.]+", "")));
        if (SharePreferenceManager.getVaule(mContext, MyLocationManager.IS_LOCATION_SUCCESS)) {
            params.putString(AD_PARAMS.GPS_ENABLE, "1");
            params.putString(AD_PARAMS.LON, SharePreferenceManager.getVaule(mContext, MyLocationManager.LOCATIONLONGITUDE, MyLocationManager.getDefaultLOCATIONKEY()));
            params.putString(AD_PARAMS.LAT, SharePreferenceManager.getVaule(mContext, MyLocationManager.LOCATIONLATITUDE, MyLocationManager.getDefaultLATITUDE()));
            params.putString(AD_PARAMS.CITY, SharePreferenceManager.getVaule(mContext, MyLocationManager.LOCATIONKEY, "CN_1_5_1"));
            params.putString(AD_PARAMS.GPS_TIME_STAMP, SharePreferenceManager.getVaule(mContext, MyLocationManager.LOCATION_SUCCESS_TIME, ""));
        } else {
            params.putString(AD_PARAMS.GPS_ENABLE, "0");
        }
        if (!StringUtil.isEmpty(mSite)) {
            params.putString(AD_PARAMS.SITE, mSite);
        }
        params.putString(AD_PARAMS.NETWORK, getNetWorkType());
        params.putString(AD_PARAMS.DENSITY, String.valueOf(UIs.getDensity()));
        params.putString(AD_PARAMS.MAC, PhoneUtils.getMacAddress(mContext));
        params.putString(AD_PARAMS.AUID, BigDataConstants.AUID);
        params.putString(AD_PARAMS.ROOT, Utils.isRooted()?"1":"0");
        params.putString(AD_PARAMS.ORIENTATION, Utils.isOrientionLandscape(mContext)?"1":"0");
        params.putString(AD_PARAMS.LANG, Locale.getDefault().getLanguage());
        params.putString(AD_PARAMS.BROWSER_UA, browserUa);
        return params;
    }

    @Override
    public void netNull() {
        if (null != mListener) {
            mListener.onRequestFailed();
        }
        super.netNull();
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        if (null != mListener) {
            mListener.onRequestFailed();
        }
        super.dataNull(updateId, errMsg);
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        if (null != mListener) {
            mListener.onRequestFailed();
        }
        super.netErr(updateId, errMsg);
    }

    @Override
    public boolean onPreExecute() {
        return super.onPreExecute();
    }

    @Override
    public LVideoDataHull<NewAdDataBean> doInBackground() {
        return MoviesHttpApi.requestHzAd(new NewAdDataParser(), buildCommonParams());
    }

    @Override
    public void onPostExecute(int updateId, NewAdDataBean result) {
        if (null != mListener) {
            mListener.onRequestSuccess(updateId, result);
        }
    }

    public void setListener(RequestResultListener<NewAdDataBean> listener) {
        mListener = listener;
    }
}
